package com.yihu.cms.action;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yihu.cms.util.HttpUtils;
import com.yihu.cms.util.Osslog;
import com.yihu.web.bean.order.Order;
import com.yihu.web.constats.DSConstants;
import com.yihu.web.utils.FormatDateTime;
import com.yihu.web.utils.PropertiesUtil;
/***
 * 活动
 * @author Aaron
 *
 */
@Controller
public class OrderActivitiController extends BaseController{
	@Autowired
	private HttpServletRequest request;
	String activivUrl = PropertiesUtil.getInstance().getProperty("activitiurl");
	/***
	 * cms 调用
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "/artwork/order", method = RequestMethod.POST)
	public @ResponseBody Object add(@RequestBody Order order) {
		Osslog.debug("actiiti.add: "+order);
		String orderNo = FormatDateTime.toOrderNumber(new Date());//系统订单号
		order.setOrderNo(orderNo);
		String url = activivUrl + PropertiesUtil.getInstance().getProperty("starttask");
		Osslog.debug("actiiti.url: "+url);
		String result = postDataToActiviti(url, JSON.toJSONString(order));
		Osslog.debug("add.result"+result);
		return result; 
	}
	
	/***
	 * //支付成功，改变订单状态 必须参数：objectId or orderNo
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "/artwork/order/paid", method = RequestMethod.POST)
	public @ResponseBody Object paidOrder(@RequestBody Order order) {
		Osslog.debug("actiiti.pay: "+order);
		Osslog.debug("CharacterEncoding:"+request.getCharacterEncoding());
		String url = activivUrl + "/order/afterPay";
		order.setStatus(DSConstants.ORDER_STATE_PAID);
		Map<String, Object> map = setMap(order);
		String result = postDataToActiviti(url,JSON.toJSONString(map));
		Osslog.debug("paiedOrder.result: "+result);
		return result;
	}

	private Map<String, Object> setMap(Order order) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("orderNo", order.getOrderNo());
		map.put("status", order.getStatus());
		return map;
	}
	

	@RequestMapping(value = "/artwork/order/cancel", method = RequestMethod.POST)
	public @ResponseBody Object cancel(@RequestBody Order order) {
		Osslog.debug("actiiti.cancel: "+order);
		String url = activivUrl + "/order/cancel";
		order.setStatus(DSConstants.ORDER_STATE_CANCEL);
		Map<String, Object> map = setMap(order);
		String result = postDataToActiviti(url,JSON.toJSONString(map));
		Osslog.debug("cancel.result: "+result);
		return result;
	}

	/***
	 * 发货 必须参数：1,objectId or orderNo 2,express.expressCompany and expressNo
	 * 
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "/artwork/order/deliver", method = RequestMethod.POST)
	public @ResponseBody Object deliverOrder(@RequestBody Order order) {
		Osslog.debug("actiiti.deliverOrder: "+order);
		order.setStatus(DSConstants.ORDER_STATE_SHIPPED);
		String url = activivUrl + "/order/delivery";
		String result = postDataToActiviti(url,JSON.toJSONString(order));
		Osslog.debug("deliverOrder.result: "+result);
		return result;
	}
	/***
	 * 收货
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "/artwork/order/received", method = RequestMethod.POST)
	public @ResponseBody Object received(@RequestBody Order order) {
		Osslog.debug("actiiti.received: "+order);
		order.setStatus(DSConstants.ORDER_STATE_RECEIVED);
		String url = activivUrl + "/order/start/orders:1:2504";
		String result = postDataToActiviti(url,JSON.toJSONString(order));
		Osslog.debug("received.result: "+result);
		return result;
	}
	/***
	 * 修改价格
	 * 
	 * @param order
	 *            1，order.objecid,product.id,product.price
	 * @return
	 */
	@RequestMapping(value = "/artwork/order/changePrice", method = RequestMethod.PUT)
	public @ResponseBody Object changePriceOfOrder(@RequestBody Order order) {
		Osslog.debug("actiiti.changePriceOfOrder: "+order);
		String url = activivUrl + "/order/start/orders:1:2504";
		String result = postDataToActiviti(url,JSON.toJSONString(order));
		Osslog.debug("changePriceOfOrder.result: "+result);
		return result;
	}
	
	/***
	 * POST发送数据到工作流
	 * 
	 * @param url
	 * @param order
	 */
	private String postDataToActiviti(String url, String json) {
		String result = null;
		try {
			result = HttpUtils.sendPostJson(url, json);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	/***
	 * POST发送数据到工作流
	 * 
	 * @param url
	 * @param order
	 */
	private String getDataToActiviti(String url, Map params) {
		String result = null;
		try {
			result = HttpUtils.sendGet(url,params);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return result;
	}

}
